"""
( A + B ) * ( C + D ) ==> A B + C D + *
"""
import string
from stack import Stack


def infix2postfix(infix_expression):
    # 定义一个保存运算符优先级的字典
    prec = {'*': 3, '/': 3, '+': 2, '-': 2, '(': 1}

    operation_stack = Stack()
    postfix_list = []

    token_list = infix_expression.split()

    for token in token_list:
        # 操作数添加到列表末尾
        if token in string.ascii_uppercase:
            postfix_list.append(token)
        # 左括号入栈
        elif token == '(':
            operation_stack.push(token)
        # 如果标记是右括号，反复从operation_stack栈中移除元素，
        # 直到移除对应的左括号
        elif token == ')':
            top_token = operation_stack.pop()
            while top_token != '(':
                # 将从栈中取出的每一个运算符都添加到结果列表的末尾
                postfix_list.append(top_token)
                top_token = operation_stack.pop()

        else:
            while (not operation_stack.is_empty()) and (prec[operation_stack.peek()] >= prec[token]):
                postfix_list.append(operation_stack.pop())
            operation_stack.push(token)

    while not operation_stack.is_empty():
        postfix_list.append(operation_stack.pop())

    return " ".join(postfix_list)


if __name__ == "__main__":
    input_str = input("输入一个中序表达式，以空格隔开每个字符：")
    print(infix2postfix(input_str))
